var Kuo_UP_Qiniup = {
    SecretId:"",
    SecretKey:"",
    SecretHost:"",
    Bucket:"",
    Config:(config)=>{
        if(config ){
            if(config.SecretKey){
                Kuo_UP_Qiniup.SecretId = config.SecretId;
            }
            if( config.SecretKey){
                Kuo_UP_Qiniup.SecretKey = config.SecretKey;
            }
            if(config.SecretHost){
                Kuo_UP_Qiniup.SecretHost = config.SecretHost;
            }
            if(config.Bucket){
                Kuo_UP_Qiniup.Bucket = config.Bucket;
            }
        }

        return Kuo_UP_Qiniup;

    },OSSUpload:async (file,file_,$SIZE)=>{
        var $returnfile_ = LTrim(file_,'/');
        var $lujing = Buffer.from($returnfile_).toString('base64').replace(/\+/g,'-').replace(/\//g,'_');;
        var $CHUSHIURL ="/buckets/"+Kuo_UP_Qiniup.Bucket+"/objects/"+$lujing+"/uploads";
        var $putPolicy = {
            'scope': Kuo_UP_Qiniup.Bucket+":"+$returnfile_,
            'deadline':Mode("Tools").Time()+3600
        }
        var $encodedPutPolicy =  Buffer.from(JSON.stringify($putPolicy)).toString('base64').replace(/\+/g,'-').replace(/\//g,'_');
        var $encodedSign = CRYPTO.createHmac('sha1', Kuo_UP_Qiniup.SecretKey ).update($encodedPutPolicy).digest('base64').replace(/\+/g,'-').replace(/\//g,'_');
        var $upload_token =  Kuo_UP_Qiniup.SecretId+ ':' +$encodedSign+':'+$encodedPutPolicy;
        var $NEIRONG ={
            headers:{
                "Authorization":"UpToken "+$upload_token,
                "Content-Type":"application/octet-stream"
            }
        };
        var fan = await POST("https://"+Kuo_UP_Qiniup.SecretHost+$CHUSHIURL,JSON.stringify($putPolicy),$NEIRONG);
        fan = json_decode(fan);
        if(fan && fan.uploadId){
            var UPID = fan.uploadId;
            let vafff = FS.readFileSync(file);
            $NEIRONG.body = vafff;
            $NEIRONG. headers['Content-Length'] = $SIZE;
            $NEIRONG.method= 'PUT';
            $CHUSHIURL ="/buckets/"+Kuo_UP_Qiniup.Bucket+"/objects/"+$lujing+"/uploads/"+UPID+"/1";
            fan = await POST("https://"+Kuo_UP_Qiniup.SecretHost+$CHUSHIURL,null,$NEIRONG);
            fan = json_decode(fan);
            if(fan && fan.etag){
                var ETAG = fan.etag;
                $CHUSHIURL ="/buckets/"+Kuo_UP_Qiniup.Bucket+"/objects/"+$lujing+"/uploads/"+UPID;
                let parts = {
                    "parts": [
                        {
                            "partNumber": 1,
                            "etag": ETAG
                        }
                    ]
                }
                $NEIRONG ={
                    headers:{
                        "Authorization":"UpToken "+$upload_token,
                        "Content-Type":"application/json"
                    }
                    ,body:JSON.stringify(parts)
                };
                fan = await POST("https://"+Kuo_UP_Qiniup.SecretHost+$CHUSHIURL,null,$NEIRONG);
                if(fan && fan.indexOf("hash") > -1){
                    return true;
                }
            }else{
                return false;
            }
        }
        return false;

    },Upload:async (file,file_)=>{
       
        var statInfo = FS.statSync(file);
        if(statInfo.size > 5242880){
          return  await Kuo_UP_Qiniup.OSSUpload(file,file_,statInfo.size);
        }
        var $returnfile_ = LTrim(file_,'/');
        var $putPolicy = {
            'scope': Kuo_UP_Qiniup.Bucket+":"+$returnfile_,
            'deadline':Mode("Tools").Time()+3600
        }
        var $encodedPutPolicy =  Buffer.from(JSON.stringify($putPolicy)).toString('base64').replace(/\+/g,'-').replace(/\//g,'_');
        var $encodedSign = CRYPTO.createHmac('sha1', Kuo_UP_Qiniup.SecretKey ).update($encodedPutPolicy).digest('base64').replace(/\+/g,'-').replace(/\//g,'_');
        var $upload_token =  Kuo_UP_Qiniup.SecretId+ ':' +$encodedSign+':'+$encodedPutPolicy;
        var $NEIRONG = {
            'key':$returnfile_,
            'token':$upload_token,
            'file':FS.readFileSync(file)
        };
        var fan = await POST("https://"+Kuo_UP_Qiniup.SecretHost,$NEIRONG);
        if(fan && fan.indexOf("hash") > -1){
            return true;
        }
        return  false;
    }
}
module.exports = Kuo_UP_Qiniup;